import router from '@/router'
import store from "@/store"
import nProgress from 'nprogress'

//进度条
import NProgress from 'nprogress'
import 'nprogress/nprogress.css'

//配置路由全局前置守卫导航
router.beforeEach((to, from, next) => {
    //开启进度条
  NProgress.start()

    //首先判断进入路由界面是否需要登录，不需要登录直接进入
    if (to.matched.length==0 || to.matched.some(ele => ele.meta.isLogin)) {
        //然后判断是否已经登录，token值是否存在
      if (store.state.login.userinfo.token) {//登录过
        //继续判断当前的vuex里面的是否有动态导航了，没有则要获取菜单导航
        if(store.state.menu.dyMenuList.length != 0){//有导航
            next()
        }else{//没有导航
            console.log('获取导航,获取导航');
            store.dispatch("menu/getMenuList")
            .then(mybaseRoutes=>{
                console.log('获取导航,获取导航',mybaseRoutes);
                mybaseRoutes.forEach(ele => {
                    router.addRoute(ele)
                });
            })
            next()
        }
      } else {//无登录，去登录
        next("/login")
      }
  
    } else {//不需要登录
      next();
    }
  }
  )

  //关闭进度条
  router.afterEach(()=>{
    nProgress.done()
  })